<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\AdminGroup;
use App\Models\AdminRule;
use App\Models\AdminRuleCat;
use Illuminate\Http\Request;
use Validator;

class AdminGroupController extends  Controller{

    //权限组列表 - 页面
    public function getIndex(){
        $data = AdminGroup::orderBy('group_id')->get()->toJson();
        view()->share("data",$data);
        return view('Admin.AdminGroup.index');
    }

    //权限组新增 - 页面
    public function getAdd(){
        $data['rule'] = AdminRule::all()->toJson();     //所有权限
        $data['cat'] = AdminRuleCat::all()->toJson();   //所有权限分类

        view()->share("data",$data);
        return view('Admin.AdminGroup.add');
    }

    //权限组修改 - 页面
    public function getEdit(Request $request){
        $getData= $request->only('id');
        $map['group_id'] = $getData['id'];
        
        $data['group'] = AdminGroup::where($map)->first()->toArray();       //当前权限组
        $data['rule'] = AdminRule::all()->toJson();                         //所有权限
        $data['cat'] = AdminRuleCat::all()->toJson();                       //所有权限分类
        $data['rule_val'] = $data['group']['rule'];                         //当前权限
        $data['group'] = json_encode($data['group']);

        view()->share("data",$data);
        return view('Admin.AdminGroup.add');

    }

    //权限组新增/修改 - 执行
    public function postHandle(Request $request){
        $postData= $request->all();
        $rules = [                      //规则
            'title' => 'required',
            'rule' => 'required',
            'miaoshu' => 'required'
        ];
        $messages = [                   //验证失败时的错误消息
            'title.required' => '权限组名不能为空！',
            'rule.required' => '请选择权限规则！',
            'miaoshu.required' => '权限描述不能为空！'
        ];
        $validation = Validator::make($postData,$rules,$messages);  //执行验证

        if($validation->fails()){   //判断验证是否成功
            return retArray(404,$validation->errors()->first());    //返回验证错误信息
        }
        $data['title'] = $postData['title'];
        $data['rule'] = str_replace(" ","",str_replace("\r\n","",$postData['rule']));
        $data['miaoshu'] = $postData['miaoshu'];

        if(empty($postData['group_id'])){   //新增
            $ret = AdminGroup::insert($data);
            if($ret===true){
                return retArray(200,"添加成功！");
            }else{
                return retArray(404,"添加失败！");
            }
        }else{                              //修改
            $ret = AdminGroup::where(["group_id"=>$postData['group_id']])->update($data);
            if($ret===1){
                return retArray(200,"修改成功！");
            }else{
                return retArray(404,"修改失败！");
            }
        }
    }

    //删除权限组 - 执行
    public function getDel(Request $request){
        $getData= $request->only('id');
        //判断权限组下是否有id
        $count = Admin::where(["group_id"=>$getData['id']])->count();
        if($count!==0){
            return retArray(200,"该权限组下还有用户，不可删除！");
        }
        //执行删除
        $ret = AdminGroup::destroy($getData['id']);
        if($ret===1){
            return retArray(200,"删除成功！");
        }else{
            return retArray(404,"删除失败！");
        }
    }

}